package phoenix.web.test_objects;

import phoenix.core.PhoenixManager;
import phoenix.core.base.extensions.TestBaseWebExt;
import phoenix.web.test_objects.data_providers.LoginDataProviders;

/**
 * Class contains test methods for Index page
 */
public class LoginTests extends TestBaseWebExt
{
	public LoginTests(PhoenixManager p_phoenix)
	{
		super(p_phoenix);
	}

	public LoginDataProviders dataProviders = new LoginDataProviders(manager);

	/**
	 * Login test (confirmed user)
	 * @param p_email
	 * @param p_password
	 */
	public void loginConfirmedTest(String p_email, String p_password)
	{
		try
		{
			page().closeSession();

			if (manager.isNF())
			{
				page().indexPage().closeIndexPopup();
			}

			if (manager.isFlirt() || manager.isFGF() || manager.isBN() || manager.isNF() || manager.isMD())
			{
				page().indexPage().openLoginForm();
			}

			page().indexPage().fillLoginForm(p_email, p_password);
			page().indexPage().submitLoginForm();
			page().searchPage().waitForPageLoad();
		}
		catch (Exception p_ex)
		{
			log().errorAndScreen("loginConfirmed: " + p_ex.toString());
		}

		test().forErrors();
	}

	/**
	 * Login test (unconfirmed user)
	 * @param p_email
	 * @param p_password
	 */
	public void loginUnConfirmedTest(String p_email, String p_password)
	{
		try
		{
			page().closeSession();

			if (manager.isNF())
			{
				page().indexPage().closeIndexPopup();
			}

			if (manager.isFlirt() || manager.isFGF() || manager.isBN() || manager.isNF() || manager.isMD())
			{
				page().indexPage().openLoginForm();
				wd().wait(1); // required in chrome for js load
			}

			page().indexPage().fillLoginForm(p_email, p_password);
			page().indexPage().submitLoginForm();
			page().welcomePage().waitForPageLoad();
		}
		catch (Exception p_ex)
		{
			log().errorAndScreen("loginUnConfirmedTest: " + p_ex.toString());
		}

		test().forErrors();
	}

	/**
	 * Login form forgot password test
	 * @param p_email
	 */
	public void forgotPasswordTest(String p_email)
	{
		try
		{
			page().closeSession();

			if (manager.isNF())
			{
				page().indexPage().closeIndexPopup();
			}

			if (manager.isFlirt() || manager.isFGF() || manager.isBN() || manager.isNF() || manager.isMD())
			{
				page().indexPage().openLoginForm();
				wd().wait(1); // required for js load in chrome
			}

			if (manager.isLGW())
			{
				page().indexPage().setLoginFormEmail(p_email);
				page().indexPage().clickLoginFormForgotPassword();
			}

			if (manager.isBN() || manager.isCNF() || manager.isFGF() || manager.isFlirt() || manager.isNF() || manager.isWB() || manager.isMD())
			{
				page().indexPage().clickLoginFormForgotPassword();
				page().indexPage().setLoginFormEmail(p_email);
				page().indexPage().clickLoginFormSend();
			}
		}
		catch (Exception p_ex)
		{
			log().errorAndScreen("forgotPasswordTest: " + p_ex.toString());
		}

		test().forErrors();
	}
}